home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / eText5 / Source / unused4Code / Slides.bproj / eTSlide.m < prev    next >
Encoding:
Text File  |  1994-07-29  |  10.2 KB  |  398 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //    FILENAME:    eTSlide.m 
  3. //    SUMMARY:    Implementation of slide-show annotations to eText documents
  4. //    SUPERCLASS:    Object
  5. //    INTERFACE:    None
  6. //    PROTOCOLS:    <DocNotification, Annotation, Tool, ASCIISupport>
  7. //    AUTHOR:        Rohit Khare
  8. //    COPYRIGHT:    (c) 1994 California Institure of Technology, eText Project
  9. ///////////////////////////////////////////////////////////////////////////////
  10. //    DESCRIPTION
  11. //        counter = an integer, initially 0
  12. //        theImage = initially 0.tiff or 0.eps
  13. //    on click: play counter.snd if it exists; wait; counter++; display new image
  14. //    on cmd-click: counter--; display; play;
  15. ///////////////////////////////////////////////////////////////////////////////
  16. //    HISTORY
  17. //    03/10/94:    Created. Influenced by the design of eTImage.
  18. ///////////////////////////////////////////////////////////////////////////////
  19.  
  20. #import "eTSlide.h"
  21. #define    _eTSlideVERSION    10
  22.  
  23. @implementation eTSlide
  24. + toolAwake:theApp
  25. {
  26.     char        buf[MAXPATHLEN];
  27.     NXBundle    *bundle;
  28.     NXImage        *theIcon;
  29.     
  30.     bundle = [NXBundle bundleForClass:[eTSlide class]];
  31.     if ( [bundle getPath:buf forResource:"eTSlideIcon" ofType:"tiff"] ) {
  32.         theIcon = [[NXImage alloc] initFromFile:buf];
  33.         [theIcon setName:"eTSlideIcon"];
  34.     } else {
  35.         NXLogError("Image not found: eTSlideIcon");
  36.         theIcon = nil;
  37.     }
  38.     [theApp   registerAnnotation: [eTSlide class] 
  39.                             name: "eTSlide"
  40.                     RTFDirective: "eTSlide"
  41.                        menuLabel: "Insert Slide Show..."
  42.                          menuKey: '\0'
  43.                         menuIcon: (NXImage *) theIcon];
  44.     [theApp registerType:NXCreateFileContentsPboardType("slideshow") 
  45.             for:[eTSlide class]];
  46.     return self;
  47. }
  48.  
  49. - init
  50. {
  51.     char        buf[MAXPATHLEN];
  52.     NXBundle   *bundle;
  53.     
  54. // PHASE I: Bookkeeping
  55.     [super init];
  56.     counter = -1;
  57.     sprintf(buf, "Untitled Show %d", [NXApp uniqueID]);
  58.     theName = NXUniqueString(buf);
  59.     isLinked = NO;
  60.     bundle = [NXBundle bundleForClass:[eTSlide class]];
  61.     //theImage = [NXImage alloc] init];
  62.     theSound = [[Sound alloc] init];
  63.     icon = theImage = [NXImage findImageNamed:"eTSlideIcon"];
  64.     [theImage setScalable:YES];
  65.     [theImage setDataRetained:YES];
  66.     [theImage setEPSUsedOnResolutionMismatch:YES];
  67.     return self;
  68. }
  69.  
  70. - initFromPboard:thePboard inDoc:theDoc linked:(BOOL) linked
  71. {
  72.     [self init];
  73.     etDoc = theDoc;
  74.     [etDoc registerNotification:self];
  75. // PHASE II: Parsing out data
  76.     isLinked = linked;
  77.     if (thePboard != nil)
  78.         if ([thePboard findAvailableTypeFrom:&NXFilenamePboardType num:1]) 
  79.         {
  80.             char    *path;
  81.             int        len;
  82.  
  83.             isLinked = linked;
  84.             [thePboard readType:NXFilenamePboardType data:&path length:&len];
  85.             if (index(path, '\t')) *index(path, '\t')=0;
  86.             //theImage = [theImage initFromFile:path];
  87.             //if ([theImage name])
  88.             //    theName = NXUniqueString([theImage name]);
  89.             //else 
  90.             if (isLinked)
  91.                 theName = NXUniqueString(path);
  92.             else theName = NXUniqueString(rindex(path, '/')+1);
  93.             [thePboard deallocatePasteboardData:path length:len];
  94.         }
  95.     return self;
  96. }
  97.  
  98. - free 
  99. {
  100.     [[NXApp inspector] invalidate]; 
  101.     [etDoc unregisterNotification:self];
  102.     etDoc=nil;
  103.     if (![theImage name] || strcmp([theImage name],"eTSlideIcon")) [theImage free];
  104.     [theSound stop];
  105.     [NXApp delayedFree:theSound];
  106.     return [super free];
  107. }
  108.  
  109. - calcCellSize:(NXSize *)theSize
  110. {
  111.     if (theImage)[theImage getSize:theSize];
  112.     else [icon getSize:theSize];
  113.     return self;
  114. }
  115.  
  116. - drawSelf:(const NXRect *)cellFrame inView:controlView
  117. {
  118.     NXPoint    point;
  119.     NXSize    theSize;
  120.     
  121.     if(!etDoc) {  
  122.          eTextObj = controlView;
  123.         if (!etDoc) etDoc = [eTextObj etDoc];
  124.         [etDoc registerNotification:self];
  125.       }
  126.   /*
  127.    * the text object expects us not to modify the current graphics state, so
  128.    * we'll save it
  129.    */
  130.     PSgsave();
  131.     
  132.   /* fill our bounds with the text object's background gray (if opaque) */
  133.     if ([controlView isOpaque]) {
  134.     PSsetgray([controlView backgroundGray]);
  135.     NXRectFill(cellFrame);
  136.     }
  137.     
  138.   /* we're in a flipped coordinate system */
  139.     point = cellFrame->origin;
  140.     point.y += cellFrame->size.height;
  141.     
  142.   /* draw the image */
  143.     [theImage getSize:&theSize];
  144.     if ((theSize.width && theSize.height))
  145.         [theImage composite:NX_SOVER toPoint:&point];
  146.     else [icon composite:NX_SOVER toPoint:&point];
  147.   /* restore the graphics state */
  148.     PSgrestore();
  149.     
  150.     return self;
  151. }
  152.  
  153. - highlight:(const NXRect *)cellFrame inView:view lit:(BOOL)flag
  154. {
  155.     static int highlighted;
  156.     if (highlighted != flag) {
  157.         highlighted = flag;
  158.       /* toggle highlighting */
  159.         //NXHighlightRect(cellFrame);
  160.     }
  161.     
  162.     return self;
  163. }
  164.  
  165. - writeComponentToPath:(NXAtom)path inFormat:(int) theFormat
  166. {    
  167.     if (isLinked) return self;
  168.     if (theFormat != ETFD_FORMAT) return self;
  169.     [etDoc registerComponent:NXUniqueString(theName)];
  170.     return self;
  171. }
  172. - readRichText:(NXStream *)stream forView:view 
  173. {
  174.     int    i;
  175.     char buf[MAXPATHLEN];
  176.     
  177.     NXScanf(stream, "%d ", &i);
  178.     if (i != _eTSlideVERSION) {
  179.         // bad version block.
  180.      NXLogError("eTSlide found unparseable version %d at position %d",
  181.                     i, NXTell(stream));
  182.         return nil;
  183.     }
  184.     
  185.     eTextObj = view;
  186.     if (!etDoc) etDoc = [eTextObj etDoc];
  187.     [etDoc registerNotification:self];
  188.     NXScanf(stream, "%c %d ", &isLinked, &i);
  189.     isLinked -= 'A';
  190.     if (i) NXRead(stream, buf, i);
  191.     buf[i] = 0;
  192.     theName = NXUniqueString(buf);
  193.     if (!isLinked && access(buf,F_OK|R_OK)) {
  194.         sprintf(buf, "%s/%s", [[etDoc navinfo] path],theName);
  195.         if (access(buf,F_OK|R_OK)) {
  196.                 return self;
  197.         }
  198.     }
  199.     //[theImage initFromFile:buf];
  200.     return self;
  201. }
  202.  
  203. - writeRichText:(NXStream *)stream forView:view
  204. {
  205.     if(!isLinked)
  206.         NXPrintf(stream, "%d %c %d %s ", _eTSlideVERSION,
  207.              isLinked ? 'B':'A',strlen(theName), theName);
  208.     return self;
  209. }
  210.  
  211. - writeASCIIRef:(NXStream *)stream forView:view
  212. {
  213.     NXPrintf(stream, "The slide show animation named %s", theName);
  214.     return self;
  215. }
  216.  
  217. #define mask (NX_LMOUSEUPMASK|NX_LMOUSEDRAGGEDMASK)
  218. #define Shift(e) (e->flags&(NX_NEXTLSHIFTKEYMASK|NX_NEXTRSHIFTKEYMASK))
  219. #ifndef abs
  220. #define abs(x) (((x)<0)? -(x) : (x))
  221. #endif
  222.  
  223. int eTSlide_mouseMoved(NXPoint *o, int n) {
  224.     /* true if mouse moves > n pixels from 'o' */
  225.     NXEvent *e;
  226.     NXPoint p;
  227.     float d;
  228.     do {
  229.         e = [NXApp getNextEvent:mask];
  230.         p = e->location;
  231.         d = abs(p.x-o->x);
  232.         if (d < abs(p.y-o->y)) d = abs(p.y-o->y);
  233.     } while (e->type != NX_LMOUSEUP && d<n);
  234.     *o = p;
  235.     return e->type != NX_LMOUSEUP;
  236. }
  237.  
  238. - (NXDragOperation)draggingSourceOperationMaskForLocal:(BOOL)flag
  239. {
  240.     if (isLinked)
  241.         return NX_DragOperationLink;
  242.     else return NX_DragOperationAll;
  243. }
  244.  
  245. - (BOOL)     trackMouse:(NXEvent *)event
  246.             inRect:(const NXRect *)cellFrame
  247.             ofView:controlView
  248. {
  249.     NXPoint    hitPoint, preHitPoint;
  250.     int        oldMask;
  251.     NXEvent    saveEvent;
  252.     Pasteboard    *dragPasteboard;
  253.     NXPoint mouseLocation;
  254.     
  255.     mouseLocation = event->location;
  256.     [controlView convertPoint:&mouseLocation fromView:NULL];
  257.     oldMask = [[controlView window] eventMask];
  258.     [[controlView window] setEventMask:(oldMask|mask)];
  259.     saveEvent = *event;
  260.     preHitPoint = event->location;
  261.     hitPoint = event->location;
  262.  
  263.     if (NXPointInRect(&mouseLocation, cellFrame)) {
  264.         if (event->data.mouse.click == 2){
  265.             if ([self respondsTo:@selector(doubleClick:)])
  266.                 [self doubleClick:self];
  267.         }
  268.         else if (event->data.mouse.click == 1) {
  269.             if (0) { //(eTSlide_mouseMoved(&hitPoint,4)) 
  270.             NXRect    dragRect;
  271.             char    filename[MAXPATHLEN];
  272.             
  273.             [controlView convertPoint: &hitPoint fromView: nil];
  274.             dragRect = *cellFrame;
  275.             dragPasteboard = [Pasteboard newName: NXDragPboard];
  276.           [dragPasteboard declareTypes:&NXFilenamePboardType num:1 owner:self];
  277.             sprintf(filename, "%s", theName);
  278.             if (access(filename,F_OK|R_OK)) {
  279.                 sprintf(filename, "%s/%s", [[etDoc navinfo] path],theName);
  280.             }
  281.             [dragPasteboard writeType: NXFilenamePboardType data: filename
  282.                 length: strlen(filename)];
  283.             hitPoint.y += 6;
  284.             preHitPoint.x = 0.0;
  285.             preHitPoint.y = 0.0;
  286.             [controlView
  287.                 dragImage:[[Application workspace] getIconForFile:filename]
  288.                 at:&(hitPoint) offset:&preHitPoint
  289.                 event:&saveEvent pasteboard:dragPasteboard
  290.                 source:self slideBack: YES];
  291.         } else if (event->flags && NX_COMMANDMASK){
  292.             if ([self respondsTo:@selector(commandClick:)])
  293.                 [self commandClick:self];
  294.             } else {
  295.                 if ([self respondsTo:@selector(click:)])
  296.                 [self click:self];
  297.             }
  298.         }
  299.     }
  300.     [[controlView window] setEventMask:oldMask];
  301.     return YES;
  302. }
  303.  
  304. - resetName:(NXAtom)string
  305. {
  306.     theName = string;
  307.     return self;
  308. }
  309.  
  310. - resetPath:(NXAtom)newPath
  311. {
  312.     return self;
  313. }
  314.  
  315. - image {return theImage;}
  316. - (NXAtom) theName {return theName;}
  317. - doc {return etDoc;}
  318. - (BOOL) isLinked {return isLinked;}
  319. - setLinked:(BOOL)l {isLinked=l; return self;}
  320.  
  321. - click:sender
  322. {
  323.     char buf[MAXPATHLEN];
  324.     // relies on -1 flag value
  325.     if (counter == -1) {
  326.         counter++;
  327.         // load image 0
  328.         sprintf(buf,"%s/%d.tiff", theName, counter);
  329.         if (access(buf, F_OK|R_OK))
  330.             sprintf(buf,"%s/%d.eps", theName, counter);
  331.         if (access(buf, F_OK|R_OK))
  332.             return self;
  333.         [theImage free];
  334.         theImage = [[NXImage alloc] initFromFile:buf];
  335.         [theImage setScalable:YES];
  336.         [theImage setDataRetained:YES];
  337.         [theImage setEPSUsedOnResolutionMismatch:YES];
  338.         [[[etDoc docUI] eTextObj] display];
  339.         
  340.         // play sound 0
  341.         sprintf(buf,"%s/%d.snd", theName, counter);
  342.         if (!access(buf, F_OK|R_OK)){
  343.             [theSound readSoundfile:buf];
  344.             [theSound play];
  345.         }    
  346.     } else {
  347.         // play sound counter
  348.         sprintf(buf,"%s/%d.snd", theName, counter);
  349.         if (!access(buf, F_OK|R_OK)){
  350.             [theSound readSoundfile:buf];
  351.             [theSound play];
  352.         }    
  353.  
  354.         // load image counter++
  355.         counter++;
  356.         sprintf(buf,"%s/%d.tiff", theName, counter);
  357.         if (access(buf, F_OK|R_OK))
  358.             sprintf(buf,"%s/%d.eps", theName, counter);
  359.         if (access(buf, F_OK|R_OK))
  360.             {counter = -1; return self;}
  361.         [theImage free];
  362.         theImage = [[NXImage alloc] initFromFile:buf];
  363.         [theImage setScalable:YES];
  364.         [theImage setDataRetained:YES];
  365.         [theImage setEPSUsedOnResolutionMismatch:YES];
  366.         [[[etDoc docUI] eTextObj] display];
  367.     }
  368.     return self;
  369. }
  370.  
  371. - commandClick: sender
  372. {
  373.     char buf[MAXPATHLEN];
  374.  
  375.     if (counter == 0) return self;
  376.     counter--;
  377.     // load image
  378.     sprintf(buf,"%s/%d.tiff", theName, counter);
  379.     if (access(buf, F_OK|R_OK))
  380.         sprintf(buf,"%s/%d.eps", theName, counter);
  381.     if (access(buf, F_OK|R_OK))
  382.         {return self;}
  383.     [theImage free];
  384.     theImage = [[NXImage alloc] initFromFile:buf];
  385.     [theImage setScalable:YES];
  386.     [theImage setDataRetained:YES];
  387.     [theImage setEPSUsedOnResolutionMismatch:YES];
  388.     [[[etDoc docUI] eTextObj] display];
  389.     // play sound
  390.     sprintf(buf,"%s/%d.snd", theName, counter);
  391.     if (!access(buf, F_OK|R_OK)){
  392.         [theSound readSoundfile:buf];
  393.         [theSound play];
  394.     }    
  395.     return self;
  396. }
  397.  
  398. @end